<?php

namespace Mentees\Controller;



class ProfileController extends MainController {



    public function index(){
    	$account = M('Member')->where('userid='.$this->userID)->find();
        $this->assign('account',$account);
        $state = M('States')->select();
        $this->assign('state',$state);
        $nowYear = date('Y')-10;
        $this->assign('nowYear',$nowYear);
      	$this->display();
    }



    public function save(){
    	$obj = M('Mentees');
        $map['userid'] = $this->userID;
        $list = $obj->where($map)->find();
        $data['firstname'] = I('post.firstname');
        $data['lastname'] = I('post.lastname');
        $data['nickname'] = I('post.nickname');
    	$data['gender'] = I('post.gender');
    	$data['birthday'] = I('post.birthday');
    	$data['address'] = I('post.address');
        $data['address2'] = I('post.address2');
        $data['phone'] = I('post.phone');
        $data['city'] = I('post.city');
        $data['state'] = I('post.state');
        $data['zip'] = I('post.zip');
        $data['school'] = I('post.school');
        $data['major'] = I('post.major');
    	$data['desired'] = I('post.desired');
    	$data['description'] = I('post.description');
    	$data['updateTime'] = time();
        if ($list) {
        	$obj->where($map)->save($data);
        }else{     	
        	$data['userid']=$this->userID;
        	$data['createTime'] = time();
        	$obj->add($data);
        }
        $email=I("post.email");
        if($email){
            unset($map);
            $map['userid']=array("neq",$this->userID);
            $map['username']=array("eq",$email);
            if(M("Member")->where($map)->find()){
                 echo $this->echo_json_str("Email has been registered");exit();
            }else{
                unset($map);
                $map['userid']=array("eq",$this->userID);
                M("Member")->where($map)->setField("username",$email);
                cookie('userName',\Think\Crypt\Driver\Xxtea::encrypt($email,C('cookie_fix')));
            }
        }
    	$state = 'SUCCESS';
        $message = L('_action_success');
        if ($this->userBasic==0) {
            $url = U('Profile/success');
        }else{
            $url = U('Index/index');
        }
        
        echo $this->echo_json_str($state,$message,$url); 
    }



    public function checknick(){

        $nick=I("nick");

        $map['nickname']=$nick;

        $map['userid']=array("neq",$this->userID);

        if(M("Mentees")->where($map)->find()){

            echo 1;

        }else{

            echo 0;

        }

    }



    //编辑密码

    public function setpsd(){

        if($_POST){

            $oldPwd=I("oldpassword");

            $password=I("password");

            $map['userid'] = $this->userID;

            $list = M("Member")->where($map)->find();

            if($list['password']!=md5($oldPwd)){

                echo $this->echo_json_str("Old password input error");exit();

            }else{

                if(M("Member")->where($map)->setField("password",md5($password))){

                    cookie(null); 

                    echo $this->echo_json_str("SUCCESS",L("_action_success"),U("Home/Login/index"));exit();

                }else{

                    echo $this->echo_json_str("Your new password can't be your old password");exit();

                }

            }

        }else{

            $this->display();

        }

    }

}